<?php
namespace Ktadmin\ChatGLM\Src;

use Ktadmin\ChatGLM\Ktadmin;

/**
 * 聊天
 */
class Chat
{
	private $ktadmin;
	private $temperature = 0.95; 
	private $top_p = 0.7; 
	private $incremental = true; 
	private $model = "chatglm_std"; 
	public function __construct(Ktadmin $ktadmin = null)
    {
        $this->ktadmin = $ktadmin;

    }
    /**
     * 初始化配置
     */
    public function initConfig($config)
    {
        
        if(isset($config['temperature']) && $config['temperature']){
            $this->temperature = (float) $config['temperature'];
        }
        if(isset($config['top_p']) && $config['top_p']){
            $this->top_p = (float) $config['top_p'];
        }
        if(isset($config['model']) && $config['model']){
            $this->model = $config['model'];
        } 
        if(isset($config['incremental']) && $config['incremental']){
            $this->incremental = $config['incremental'];
        } 


    }
     /**
	 * 执行获取 
	 * @param text string  文本内容
	 * @param history array  会话历史,只支持偶数，Q A Q A 的形式传进去
	 * @return  JSON
	 */
    public function sendText($messages = [], $callback, $config = [])
    {
    	$this->initConfig($config);
    	$param = [
			'prompt' => $messages,
			'temperature' => $this->temperature,
			'top_p' => $this->top_p,
			// 'incremental' => $this->incremental,
		];
		$url = 'https://open.bigmodel.cn/api/paas/v3/model-api/'.$this->model.'/sse-invoke';
		$header = ["Authorization:".$this->ktadmin->access_token,"Content-Type:application/json"];
		return $this->ktadmin->curlRequest($url,'POST',json_encode($param),$header,$callback);
    }
}